package page.transport;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import utils.Utils;
import app.WebApp;
import dao.BindingDao;
import dao.RouteDao;
import entity.Route;

public class RouteListPage extends WebPage {

	public RouteListPage() {
		super();

		final RouteDao rDao = new RouteDao(WebApp.DATA_XML_FOLDER);
		final BindingDao bDao = new BindingDao(WebApp.DATA_XML_FOLDER);

		final List<Route> allRoute = rDao.getAll();

		add(new DataView<Route>("table-rows", new ListDataProvider<Route>(allRoute)) {
			@Override
			protected void populateItem(final Item<Route> item) {
				final Route route = item.getModelObject();
				item.add(new Label("start", route.getStart()));
				item.add(new Label("end", route.getEnd()));
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						
						Utils.delete(route);
						setResponsePage(new RouteListPage());
					}
				});
			}
		});

		add(new Link("createLink") {
			@Override
			public void onClick() {
				setResponsePage(new RoutePage(new Route()));
			}
		});
		add(new Link("back") {
			@Override
			public void onClick() {
				setResponsePage(new Ways());
			}
		});
	}
}
